package org.seven.cpcrkapp.util;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import org.seven.cpcrkapp.App;

public abstract class DialogUtils {
    public static String ERROR_MSG = "msg";

    public static ProgressDialog createProgressDialog(Context context) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("操作中...");
        return progressDialog;
    }

    public static void showItemsDialog(Context context, final String[] list, final EditText editText) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("请选择");
        builder.setItems(list, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                editText.setText(list[which]);
            }
        });
        builder.show();
    }

    public static void showDateDialog(Context context, final EditText editText) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                editText.setText(DateUtils.getString(year, month, dayOfMonth));
            }
        }, DateUtils.getYear(), DateUtils.getMonth(), DateUtils.getDay());
        datePickerDialog.show();
    }

    public static void alert(String msg) {
        Toast.makeText(App.getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    public static void alert(int resId){
        DialogUtils.alert(App.getContext().getString(resId));
    }

    public static void alert(Message message) {
        DialogUtils.alert(message.getData().getString(ERROR_MSG));
    }
}
